package com.uchengwang.ui.cm_ui.rychat.msgtype;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.uchengwang.R;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.AutoLinkTextView;
import io.rong.imkit.widget.ILinkClickListener;
import io.rong.imkit.widget.LinkTextViewMovementMethod;
import io.rong.imkit.widget.provider.TextMessageItemProvider;
import io.rong.imlib.model.Message;
import io.rong.message.TextMessage;

/**
 * Created by U城网 on 2017/11/14.
 */
@ProviderTag(
        messageContent = TextMessage.class,
        showReadState = true
)
public class TextMsgItemProvider extends TextMessageItemProvider {
    @Override
    public void bindView(final View v, int i, TextMessage textMessage, final UIMessage data) {
        ViewHolder holder = (ViewHolder)v.getTag();
        if(data.getMessageDirection() == Message.MessageDirection.SEND) {
            holder.message.setBackgroundResource(R.drawable.rc_ic_bubble_right);
            holder.message.setTextColor(v.getResources().getColor(R.color.white));
        } else {
            holder.message.setBackgroundResource(R.drawable.rc_ic_bubble_left);
            holder.message.setTextColor(v.getResources().getColor(R.color.black_color));
        }

        final AutoLinkTextView textView = holder.message;
        if(data.getTextMessageContent() != null) {
            int len = data.getTextMessageContent().length();
            if(v.getHandler() != null && len > 500) {
                v.getHandler().postDelayed(new Runnable() {
                    public void run() {
                        textView.setText(data.getTextMessageContent());
                    }
                }, 50L);
            } else {
                textView.setText(data.getTextMessageContent());
            }
        }

        holder.message.setMovementMethod(new LinkTextViewMovementMethod(new ILinkClickListener() {
            public boolean onLinkClick(String link) {
                RongIM.ConversationBehaviorListener listener = RongContext.getInstance().getConversationBehaviorListener();
                boolean result = false;
                if(listener != null) {
                    result = listener.onMessageLinkClick(v.getContext(), link);
                }

                if(listener == null || !result) {
                    String str = link.toLowerCase();
                    if(str.startsWith("http") || str.startsWith("https")) {
                        Intent intent = new Intent("io.rong.imkit.intent.action.webview");
                        intent.setPackage(v.getContext().getPackageName());
                        intent.putExtra("url", link);
                        v.getContext().startActivity(intent);
                        result = true;
                    }
                }

                return result;
            }
        }));
    }


    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_text_message, (ViewGroup)null);
        ViewHolder holder = new ViewHolder();
        holder.message = (AutoLinkTextView)view.findViewById(16908308);
        view.setTag(holder);
        return view;
    }

      class ViewHolder {
        AutoLinkTextView message;
        boolean longClick;

    }
}
